<?php

namespace App\CompanyApi\Controllers;


use App\CompanyApi\Requests\JobpositionRequest;
use App\CompanyApi\Resources\JobpositionDetailResource;
use App\CompanyApi\Resources\JobpositionResource;
use App\CompanyApi\Services\Jobposition\JobpositionService;
use App\Enums\ResponseCode;
use App\Exceptions\ApiExceptions;
use App\Http\Controllers\Api\AbstractApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class JobpositionController extends AbstractApiController
{
    public function list(Request $request, JobpositionService $service)
    {
        $input = $request->validate([
            'cur_status' => 'int|min:1',
            'name' => '',
        ]);
        $result = $service->list(
            company_id: auth('company-api')->id(),
            cur_status: $input['cur_status']??0,
            name: $input['name']??''
        );
        return $this->success(trans("success"), ResponseCode::OK, new JobpositionResource($result));
    }

    /**
     * 职位详情
     * @param JobpositionService $service
     * @param $id
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function detail(JobpositionService $service,$id)
    {
        $result = $service->detail(company_id: auth('company-api')->id(),id: $id);
        return $this->success(trans("success"), ResponseCode::OK, new JobpositionDetailResource($result));
    }
    /**
     * 统计
     * @param JobpositionService $service
     * @return \Illuminate\Http\JsonResponse
     */
    public function statistics(JobpositionService $service)
    {
        $result = $service->statistics(auth('company-api')->id());
        return $this->success(trans("success"), ResponseCode::OK, $result);
    }

    /**
     * 创建职位
     * @param JobpositionRequest $request
     * @param JobpositionService $service
     * @return JsonResponse
     * @throws ApiExceptions
     */
    public function store(JobpositionRequest $request,JobpositionService $service)
    {
        $service->store(
            company_id: auth('company-api')->id(),input: $request->validated()
        );
        return $this->success(trans("success"), ResponseCode::OK);
    }
}